package com.example.utils;

import android.content.Context;
import android.util.Log;



import org.apache.commons.lang3.StringUtils;

public class StringUtil {



    public static boolean isEmpty(Object obj) {
        return obj == null || obj.toString().trim().equals("");
    }

    public static boolean isJson(String str) {
        boolean result = false;
        if (StringUtils.isNotBlank(str)) {
            str = str.trim();
            if (str.startsWith("{") && str.endsWith("}")) {
                result = true;
            } else if (str.startsWith("[") && str.endsWith("]")) {
                result = true;
            }
        }
        return result;
    }

    /**
     * 过滤null和 空串
     */
    public static String filter(String target){
        if(target == null ){
            return "";
        }
        return target.trim();
    }

    /**
     * According the id to find the string.
     * @param context
     * @param id
     * @return
     */
    public static String getStringByID(Context context, int id){
        try{
            return context.getResources().getString(id);
        }catch (Exception e) {
            Log.e("", e.getMessage());
            return null ;
        }
    }

    /**
     * 将路径转换为uri
     * @param path
     * @return
     */
    public static String convertPath2URI(String path){
        if( !StringUtil.isEmpty(path)){
            return "file:///"+ path;
        }
        return path ;
    }
}
